package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.dto.PictureAddNewDTO;
import cn.tedu.csmall.product.service.PictureService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 处理图片相关请求的控制器
 *
 * @author Chen
 * @since 2023/2/11 9:22:22
 */
@Slf4j
@RestController
@RequestMapping("/pictures")
@Tag(name = "03. 图片管理模块")
@Validated
public class PictureController {

    private final PictureService pictureService;

    @Autowired
    public PictureController(PictureService pictureService) {
        log.debug("创建控制器对象：PictureController");
        this.pictureService = pictureService;
    }

    // http://localhost:8088/pictures/add-new
    @ApiOperationSupport(order = 100)
    @Operation(summary = "添加图片")
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(@Valid PictureAddNewDTO pictureAddNewDTO) {
        log.debug("开始处理【添加图片】的请求，参数：{}", pictureAddNewDTO);
        pictureService.addNew(pictureAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:8088/pictures/9527/delete
    @Parameter(name = "id", description = "图片ID", required = true, example = "9527",
            schema = @Schema(type = "integer", format = "int64"))
    @ApiOperationSupport(order = 100)
    @Operation(summary = "删除图片")
    @PostMapping("/{id}/add-new")
    public JsonResult<Void> delete(@Range(min = 1L, max = 10000L, message = "删除图片失败，图片ID非法！") @PathVariable Long id) {
        log.debug("开始处理【删除图片】的请求，参数：{}", id);
        pictureService.deleteById(id);
        return JsonResult.ok();
    }

}
